package utility;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import dto.LeaveDTO;
import dto.LeaveSearchDTO;

public class LeaveUtil
{

	private LeaveUtil()
	{
	}

	public static int getDuration(Date start, Date end)
	{
		Calendar enddate = new GregorianCalendar();
		enddate.setTime(end);
		long d1 = enddate.getTimeInMillis();
		Calendar startdate = new GregorianCalendar();
		startdate.setTime(start);
		long d2 = startdate.getTimeInMillis();

		int noOfDays = (int) ((d1 - d2) / (60 * 60 * 24 * 1000));
		return noOfDays;
	}

	public static ArrayList<LeaveDTO> loadLeaveDTO(ResultSet result)
	{
		ArrayList<LeaveDTO> leaveList = new ArrayList<LeaveDTO>();

		try
		{
			while (result.next())
			{
				LeaveDTO chosenLeave = new LeaveDTO();
				chosenLeave.set_endDate(result.getDate("EndDate"));
				chosenLeave.set_endDateHalfDay(result
						.getString("EndDateHalfDay"));
				chosenLeave.set_leaveID(result.getInt("LeaveID"));
				chosenLeave.set_leaveType(result.getString("LeaveType"));
				chosenLeave.set_reason(result.getString("Reason"));
				chosenLeave.set_startDate(result.getDate("StartDate"));
				chosenLeave.set_startDateHalfDay(result
						.getString("StartDateHalfDay"));
				chosenLeave.set_status(result.getString("Status"));
				chosenLeave.set_userID(result.getInt("UserID"));
				chosenLeave.set_workDissemination(result
						.getString("WorkDisseminationID"));
				chosenLeave.set_userName(result.getString("UserName"));
				chosenLeave.set_applicationDate(result
						.getDate("ApplicationDate"));
				chosenLeave.set_ApprovingManID(result
						.getInt("ApprovingManager"));
				chosenLeave.set_rejectReason(result.getString("RejectReason"));
				chosenLeave.set_duration(result.getInt("Duration"));
				chosenLeave.set_LeaveSpent(result.getInt("LeaveSpent"));
				leaveList.add(chosenLeave);
			}
		}
		catch (Exception e)
		{
			System.out.println("Error Occurred at load data");
			System.out.println(e.toString());

		}
		return leaveList;
	}

	public static ArrayList<LeaveDTO> filterWithID(ArrayList<LeaveDTO> result,
			LeaveSearchDTO criti)
	{
		// ArrayList<LeaveDTO> leaveList = new ArrayList<LeaveDTO>();
		try
		{
			int size = result.size();

			for (int i = 0, posit = 0; i < size; i++)
			{
				if ((result.get(posit)).get_userID() != criti.get_userID())
				{
					result.remove(result.get(posit));
				}
				else
					posit++;
			}

		}
		catch (Exception e)
		{
			System.out.println("Filter with ID error");
			System.out.println(e.toString());
			// leaveList = null;
		}
		return result;
	}

	public static ArrayList<LeaveDTO> filterWithName(
			ArrayList<LeaveDTO> result, LeaveSearchDTO criti)
	{
		try
		{
			int size = result.size();

			for (int i = 0, posit = 0; i < size; i++)
			{
				if (!(result.get(posit).get_userName().equals(criti
						.get_userName())))
				{
					result.remove(result.get(posit));
				}
				else
					posit++;
			}

		}
		catch (Exception e)
		{
			System.out.println("Filter with Name error");
			System.out.println(e.toString());
			// leaveList = null;
		}
		return result;
	}

	public static ArrayList<LeaveDTO> filterLeaveWithStartandEndRange(
			ArrayList<LeaveDTO> result, LeaveSearchDTO criti)
	{
		try
		{
			int size = result.size();

			for (int i = 0, posit = 0; i < size; i++)
			{
				Calendar cAppDate = new GregorianCalendar();
				cAppDate.setTime(result.get(posit).get_applicationDate());

				if (!(criti.get_startDate().before(cAppDate.getTime()))
						|| !(criti.get_endDate().after(cAppDate.getTime())))
				{
					result.remove(result.get(posit));
				}

				else
					posit++;
			}
		}
		catch (Exception e)
		{
			System.out.println("find all error");
			System.out.println(e.toString());
			// return leaveList = null;
		}
		return result;
	}

	public static ArrayList<LeaveDTO> filterLeaveWithStartRange(
			ArrayList<LeaveDTO> result, LeaveSearchDTO criti)
	{

		try
		{
			int size = result.size();

			for (int i = 0, posit = 0; i < size; i++)
			{
				Calendar cAppDate = new GregorianCalendar();
				cAppDate.setTime(result.get(posit).get_applicationDate());

				if (!(criti.get_startDate().before(cAppDate.getTime())))
				{
					result.remove(result.get(posit));
				}

				else
					posit++;
			}

		}
		catch (Exception e)
		{
			System.out.println("find all error");
			System.out.println(e.toString());
			// return leaveList = null;
		}
		return result;
	}

	public static ArrayList<LeaveDTO> filterLeaveWithEndRange(
			ArrayList<LeaveDTO> result, LeaveSearchDTO criti)
	{
		// ArrayList<LeaveDTO> leaveList = new ArrayList<LeaveDTO>();
		try
		{
			int size = result.size();

			for (int i = 0, posit = 0; i < size; i++)
			{
				Calendar cAppDate = new GregorianCalendar();
				cAppDate.setTime(result.get(posit).get_applicationDate());

				if (!(criti.get_endDate().after(cAppDate.getTime())))
				{
					result.remove(result.get(posit));
				}

				else
					posit++;
			}
		}
		catch (Exception e)
		{
			System.out.println("find all error");
			System.out.println(e.toString());
			// return leaveList = null;
		}
		return result;
	}

	public static ArrayList<LeaveDTO> filterSurbodinatesOnLeave(
			ArrayList<LeaveDTO> result, LeaveSearchDTO criti)
	{
		// ArrayList<LeaveDTO> leaveList = new ArrayList<LeaveDTO>();
		try
		{
			int size = result.size();

			for (int i = 0, posit = 0; i < size; i++)
			{
				Calendar cStartDate = new GregorianCalendar();
				cStartDate.setTime(result.get(posit).get_startDate());
				// Calendar cEnd = new GregorianCalendar();
				// cEnd.setTime(result.get(posit).get_endDate());

				if (!(criti.get_startDate().before(cStartDate.getTime()))
						&& !(criti.get_endDate().after(cStartDate.getTime())))
				{
					result.remove(result.get(posit));
				}

				else
					posit++;

			}
		}
		catch (Exception e)
		{
			System.out.println("find all error");
			System.out.println(e.toString());
			// return leaveList = null;
		}
		return result;
	}

	public static ArrayList<LeaveDTO> filterLeaveWithStartandEndRangeByDate(
			ArrayList<LeaveDTO> result, LeaveSearchDTO criti)
	{
		try
		{
			int size = result.size();

			for (int i = 0, posit = 0; i < size; i++)
			{
				Calendar cStartDate = new GregorianCalendar();
				cStartDate.setTime(result.get(posit).get_startDate());

				Calendar cEndDate = new GregorianCalendar();
				cEndDate.setTime(result.get(posit).get_endDate());

				if ((!(criti.get_startDate().before(cStartDate.getTime()))
						&& !(criti.get_endDate().after(cStartDate.getTime())))
						|| (!(criti.get_startDate().before(cEndDate.getTime()))
						&& !(criti.get_endDate().after(cEndDate.getTime()))))
				{
					result.remove(result.get(posit));
				}

				else
					posit++;
			}
		}
		catch (Exception e)
		{
			System.out.println("find all error");
			System.out.println(e.toString());
			// return leaveList = null;
		}
		return result;
	}

//	public static ArrayList<LeaveDTO> filterLeaveWithStartRangeByDate(
//			ArrayList<LeaveDTO> result, LeaveSearchDTO criti)
//	{
//
//		try
//		{
//			int size = result.size();
//
//			for (int i = 0, posit = 0; i < size; i++)
//			{
//				Calendar cStartDate = new GregorianCalendar();
//				cStartDate.setTime(result.get(posit).get_startDate());
//
//				if (!(criti.get_startDate().before(cStartDate.getTime())))
//				{
//					result.remove(result.get(posit));
//				}
//
//				else
//					posit++;
//			}
//
//		}
//		catch (Exception e)
//		{
//			System.out.println("find all error");
//			System.out.println(e.toString());
//			// return leaveList = null;
//		}
//		return result;
//	}
//
//	public static ArrayList<LeaveDTO> filterLeaveWithEndRangeByDate(
//			ArrayList<LeaveDTO> result, LeaveSearchDTO criti)
//	{
//		// ArrayList<LeaveDTO> leaveList = new ArrayList<LeaveDTO>();
//		try
//		{
//			int size = result.size();
//
//			for (int i = 0, posit = 0; i < size; i++)
//			{
//				Calendar cEndDate = new GregorianCalendar();
//				cEndDate.setTime(result.get(posit).get_startDate());
//
//				if (!(criti.get_endDate().after(cEndDate.getTime())))
//				{
//					result.remove(result.get(posit));
//				}
//
//				else
//					posit++;
//			}
//		}
//		catch (Exception e)
//		{
//			System.out.println("find all error");
//			System.out.println(e.toString());
//			// return leaveList = null;
//		}
//		return result;
//	}
}
